import { Authority } from "./models/Authority";
import { MyFileDir } from "./models/MyFileDirTable";
import { User } from "./models/User";
import { UserPermissions } from "./models/UserPermissions";
import {Collect} from "./models/Collect"
import { History } from "./models/History"
import { Favourite } from "./models/Favourite"
import md5 from "md5"
import { Op } from "sequelize"

// 收藏
User.belongsToMany(MyFileDir, {
    through: {
        model: Collect,
    },
    as: "CollectMyFileDir",
    foreignKey: "userId",
    constraints: false,
});

MyFileDir.belongsToMany(User, {
    through: {
        model: Collect,
    },
    as: "CollectUser",
    foreignKey: "fileId",
    constraints: false
});


// 历史记录表
User.belongsToMany(MyFileDir, {
    through: {
        model: Favourite,
    },
    as: "FavouriteMyFileDir",
    foreignKey: "userId",
    constraints: false,
});

MyFileDir.belongsToMany(User, {
    through: {
        model: Favourite,
    },
    as: "FavouriteUser",
    foreignKey: "fileId",
    constraints: false
});


// 喜欢表
User.belongsToMany(MyFileDir, {
    through: {
        model: History,
    },
    as: "HistoryMyFileDir",
    foreignKey: "userId",
    constraints: false,
});

MyFileDir.belongsToMany(User, {
    through: {
        model: History,
    },
    as: "HistoryUser",
    foreignKey: "fileId",
    constraints: false
});



// User.sync({ alter: true });
MyFileDir.sync({ alter: true });
// Authority.sync({ alter: true });
// UserPermissions.sync({ alter: true });
// History.sync({ alter: true });
// Collect.sync({ alter: true });
// Favourite.sync({ alter: true });

async function initAuthority() {
    let count = await Authority.count();
    if (count == 0) {
        await Authority.bulkCreate([
            {
                authorityName: "root",
            },
            {
                authorityName: "vip",
            },
        ]);
    }
}

async function initUserAndPermission() {
    let count = await User.count();
    if (count == 0) {
        let res = await User.bulkCreate([
            {
                username: "root",
                password: md5("123456"),
                phone: "15651070852",
            },
            {
                username: "admin",
                password: md5("123456"),
                phone: "13701486642",
            },
        ]);
    }
}

async function grant() {
    let count = await UserPermissions.count()

    if( count != 0 ){
        return
    }
    let root = await User.findOne( {
        where: {
            username: "root",
        }
    })
    let admin = await User.findOne( {
        where: {
            username: "admin",
        }
    })
    let rootPermissions = await Authority.findAll()
    let adminPermissions = await Authority.findAll({
        where: {
            [Op.not]: {
                authorityName: "root"
            }
        }
    });
    await (root! as any).setAuthorities(rootPermissions)
    await (admin! as any).setAuthorities(adminPermissions)
}

async function init() {
    await initAuthority();
    await initUserAndPermission()
    await grant()
}
// init()


export {
    Authority,
    MyFileDir,
    User,
    UserPermissions
}